package com.xiaobai.netease_news.activities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.fragments.DiscoveryFragment;
import com.xiaobai.netease_news.fragments.NewsFragment;
import com.xiaobai.netease_news.fragments.ProfileFragment;
import com.xiaobai.netease_news.fragments.ReadFragment;
import com.xiaobai.netease_news.fragments.VaFragment;

import java.lang.ref.WeakReference;

public class MainActivity extends FragmentActivity
        implements RadioGroup.OnCheckedChangeListener {

    private NewsFragment newsFragment;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private ReadFragment readFragment;
    private VaFragment vaFragment;
    private DiscoveryFragment discoveryFragment;
    private ProfileFragment profileFragment;
    private Fragment currentFragment;
    private boolean isExit;
    private MyHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        newsFragment = new NewsFragment();
        readFragment = new ReadFragment();
        vaFragment = new VaFragment();
        discoveryFragment = new DiscoveryFragment();
        profileFragment = new ProfileFragment();
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.main_fragment_container, newsFragment, "TAG1").commit();
        currentFragment = newsFragment;

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.main_radio_group);
        RadioButton tabNews = (RadioButton) findViewById(R.id.main_tab_news);
        tabNews.setChecked(true);
        radioGroup.setOnCheckedChangeListener(this);

        handler = new MyHandler(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_tab_news:
                transaction = manager.beginTransaction();
                Fragment fragment1 = manager.findFragmentByTag("TAG1");
                if (fragment1 == null)
                    transaction.hide(currentFragment).add(R.id.main_fragment_container, newsFragment, "TAG1")
                            .show(newsFragment).commit();
                else
                    transaction.hide(currentFragment).show(fragment1).commit();
                currentFragment = newsFragment;
                break;
            case R.id.main_tab_read:
                transaction = manager.beginTransaction();
                Fragment fragment2 = manager.findFragmentByTag("TAG2");
                if (fragment2 == null)
                    transaction.hide(currentFragment).add(R.id.main_fragment_container, readFragment, "TAG2")
                            .show(readFragment).commit();
                else
                    transaction.hide(currentFragment).show(fragment2).commit();
                currentFragment = readFragment;
                break;
            case R.id.main_tab_va:
                transaction = manager.beginTransaction();
                Fragment fragment3 = manager.findFragmentByTag("TAG3");
                if (fragment3 == null)
                    transaction.hide(currentFragment).add(R.id.main_fragment_container, vaFragment, "TAG3")
                            .show(vaFragment).commit();
                else
                    transaction.hide(currentFragment).show(fragment3).commit();
                currentFragment = vaFragment;
                break;
            case R.id.main_tab_discovery:
                transaction = manager.beginTransaction();
                Fragment fragment4 = manager.findFragmentByTag("TAG4");
                if (fragment4 == null)
                    transaction.hide(currentFragment).add(R.id.main_fragment_container, discoveryFragment, "TAG4")
                            .show(discoveryFragment).commit();
                else
                    transaction.hide(currentFragment).show(fragment4).commit();
                currentFragment = discoveryFragment;
                break;
            case R.id.main_tab_profile:
                transaction = manager.beginTransaction();
                Fragment fragment5 = manager.findFragmentByTag("TAG5");
                if (fragment5 == null)
                    transaction.hide(currentFragment).add(R.id.main_fragment_container, profileFragment, "TAG5")
                            .show(profileFragment).commit();
                else
                    transaction.hide(currentFragment).show(fragment5).commit();
                currentFragment = profileFragment;
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(this, R.string.back_exit, Toast.LENGTH_SHORT).show();
                handler.sendMessageDelayed(handler.obtainMessage(), 2000);
            } else {
                this.finish();
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private static class MyHandler extends Handler {

        private final WeakReference<MainActivity> mActivity;

        public MyHandler(MainActivity activity) {
            this.mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            MainActivity activity = mActivity.get();
            if (activity != null) {
                activity.isExit = false;
            }
        }
    }
}
